//
//  BackBoxEditVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/29.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "BackBoxEditVC.h"
#import "BackBoxCell.h"
#import "TurnOverBoxModel.h"

@interface BackBoxEditVC ()<UITableViewDelegate,UITableViewDataSource> {
    BOOL isAllSelected;
}

@property (nonatomic, strong) NSMutableArray <TurnOverBoxModel *>*array;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *allBtn;

@end

@implementation BackBoxEditVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        self.allBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 20, 55, 24)];
        [self.allBtn setTitle:@"全选" forState:0];
        [self.allBtn setTitleColor:HexColor(0x2B2C44) forState:0];
        self.allBtn.titleLabel.font = BoldFontOfSize(14);
        [self.allBtn addTarget:self action:@selector(allClick) forControlEvents:UIControlEventTouchUpInside];
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice"] forState:0];
        self.allBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -4, 0, 0);
        [_bottomView addSubview:self.allBtn];
        
        UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.allBtn.nim_right + 15, 10, ScreenW - 15 - (self.allBtn.nim_right + 15), 44)];
        [editBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        editBtn.layer.masksToBounds = YES;
        editBtn.layer.cornerRadius = 6;
        [editBtn setTitle:@"变更为空置状态" forState:0];
        [editBtn setTitleColor:Color_ThemeColor forState:0];
        editBtn.titleLabel.font = BoldFontOfSize(16);
        [editBtn addTarget:self action:@selector(setClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:editBtn];
    }
    return _bottomView;
}

- (void)allClick {
    if (isAllSelected) {
        isAllSelected = NO;
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice"] forState:0];
        [self.array enumerateObjectsUsingBlock:^(TurnOverBoxModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelected = NO;
        }];
        [self.tableView reloadData];
    }else {
        isAllSelected = YES;
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice_sel"] forState:0];
        [self.array enumerateObjectsUsingBlock:^(TurnOverBoxModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelected = YES;
        }];
        [self.tableView reloadData];
    }
}

- (void)setClick {
    
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - BottomHeight - 64 - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(NSMutableArray<TurnOverBoxModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
        TurnOverBoxModel *model1 = [TurnOverBoxModel new];
        model1.boxNO = @"42369563298";
        model1.ownMaster = @"3PL公司名称";
        model1.rentMaster = @"发货方公司名称";
        model1.status = 1;
        model1.canChoose = YES;
        [_array addObject:model1];
        
        TurnOverBoxModel *model2 = [TurnOverBoxModel new];
        model2.boxNO = @"423695632782";
        model2.ownMaster = @"3PL公司名称";
        model2.rentMaster = @"发货方公司名称";
        model2.status = 1;
        model2.canChoose = YES;
        [_array addObject:model2];
        
        TurnOverBoxModel *model3 = [TurnOverBoxModel new];
        model3.boxNO = @"423695632780";
        model3.ownMaster = @"3PL公司名称";
        model3.rentMaster = @"发货方公司名称";
        model3.status = 1;
        model3.canChoose = YES;
        [_array addObject:model3];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"返箱管理";
    isAllSelected = NO;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BackBoxCell *cell = [BackBoxCell BackBoxCellWithTableView:tableView];
    TurnOverBoxModel *model = self.array[indexPath.row];
    cell.model = model;
    cell.chooseBlock = ^{
        model.isSelected = !model.isSelected;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BackBoxCell *cell = [BackBoxCell BackBoxCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
